home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / utility / logchk10.zip / LOGCHECK.C next >
C/C++ Source or Header  |  1992-08-17  |  6KB  |  187 lines

  1.  
  2.  
  3. /*
  4. This program does 3 things:
  5.   - Function -m marks the size of the specified log file
  6.   - Function -s scans any addition to the log file for the specified string.
  7.      If found, error level 1 is returned; otherwise, 0 is returned.
  8.   - Function -p prints out the new part of the log.
  9. */
  10.  
  11.  
  12. #define ERRORLEVEL_SUCCESS       0
  13. #define ERRORLEVEL_SCAN_FAILED   1
  14. #define ERRORLEVEL_BAD_PARAMETER 2
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <process.h>
  20. #include <conio.h>
  21. #include <time.h>
  22. #include <dos.h>  /* Dos-specific functions are used to get date/time. */
  23.  
  24. #include "common.h"
  25. #include "modem.h"
  26.  
  27. FILE *log_file_to_check,*mark_holding_file;
  28. char temp_string[128],
  29.      mark_holding_filespec[]="logcheck.dat",
  30.      log_file_spec[64];
  31. long file_size_marked,file_size_current;
  32.  
  33.  
  34. /*------------------------- Subroutines ------------------------------*/
  35. void print_header()
  36. {
  37. printf("LOGCHECK 1.0          Alton Moore\n");
  38. printf("                      Fidonet 1:397/5264 (NEC)\n");
  39. printf("                      WWIVnet 1@1042\n");
  40. }
  41.  
  42. void print_help_message()
  43. {
  44. printf("?Illegal number of arguments; command line syntax:     \n\n");
  45. printf("    LOGCHECK [function] [argument]                     \n\n");
  46. printf("If [function] is -m then [argument] should be the      \n");
  47. printf("log file specification; the size of this file will be  \n");
  48. printf("marked down and its name remembered.                   \n\n");
  49. printf("If [function] is -s then [argument] should be the      \n");
  50. printf("string to look for in the new piece of the log         \n");
  51. printf("file which was previously specified (with -m).         \n\n");
  52. printf("If [function] is -p then the new part of the previously\n");
  53. printf("marked file is printed to the screen.  After that, the \n");
  54. printf("program waits for [argument] seconds.                  \n\n");
  55. printf("<Return> to continue: ");
  56. gets(temp_string);
  57. }
  58.  
  59. int mark_file_size(char file_name[])
  60. {
  61. mark_holding_file = fopen(mark_holding_filespec,"wb");
  62. if (mark_holding_file == NULL)
  63.     return(ERRORLEVEL_BAD_PARAMETER);
  64. log_file_to_check = fopen(file_name,"rb");
  65. if (log_file_to_check == NULL)             /* Log file gone? */
  66.   {                                        /*  Write out 0 as the filesize. */
  67.   strcpy(temp_string,file_name);
  68.   strcat(temp_string,CRLF_string);
  69.   fwrite(temp_string,strlen(temp_string),1,mark_holding_file);
  70.   strcpy(temp_string,"0");
  71.   strcat(temp_string,CRLF_string);
  72.   fwrite(temp_string,strlen(temp_string),1,mark_holding_file);
  73.   fclose(mark_holding_file);
  74.   return(ERRORLEVEL_BAD_PARAMETER);
  75.   }
  76. fseek(log_file_to_check,0,SEEK_END);
  77. file_size_marked = ftell(log_file_to_check);
  78. fclose(log_file_to_check);
  79. strcpy(temp_string,file_name);      /* Write the filespec as the 1st line. */
  80. strcat(temp_string,CRLF_string);
  81. fwrite(temp_string,strlen(temp_string),1,mark_holding_file);
  82. ltoa(file_size_marked,temp_string,10);  /* Now write out current filesize. */
  83. strcat(temp_string,CRLF_string);
  84. fwrite(temp_string,strlen(temp_string),1,mark_holding_file);
  85. fclose(mark_holding_file);
  86. return(ERRORLEVEL_SUCCESS);
  87. }
  88.  
  89. int scan_file_for_string(char string_to_scan_for[])
  90. {
  91. char the_char;
  92. mark_holding_file = fopen(mark_holding_filespec,"rt");
  93. if (mark_holding_file == NULL)
  94.     return(ERRORLEVEL_BAD_PARAMETER);
  95. fgets(log_file_spec,sizeof(log_file_spec),mark_holding_file);
  96. log_file_spec[strlen(log_file_spec)-1] = 0;
  97. fgets(temp_string,sizeof(temp_string),mark_holding_file);
  98. temp_string[strlen(temp_string)-1] = 0;  /* Cut off LF. */
  99. file_size_marked = atol(temp_string);
  100. fclose(mark_holding_file);
  101. log_file_to_check = fopen(log_file_spec,"rb");
  102. if (log_file_to_check == NULL)
  103.     return(ERRORLEVEL_BAD_PARAMETER);
  104. fseek(log_file_to_check,0,SEEK_END);
  105. file_size_current = ftell(log_file_to_check);
  106. if (file_size_current <= file_size_marked)
  107.     return(ERRORLEVEL_SCAN_FAILED);
  108. fseek(log_file_to_check,file_size_marked,SEEK_SET);
  109. trace_start(string_to_scan_for);
  110. while (ftell(log_file_to_check) < file_size_current)
  111.     {
  112.     fread(&the_char,1,1,log_file_to_check);  /* Read 1 character. */
  113.     trace_process_received_character(the_char);
  114.     if (trace_fired(string_to_scan_for))
  115.         {
  116.         trace_stop(string_to_scan_for,0);
  117.         fclose(log_file_to_check);
  118.         return(ERRORLEVEL_SUCCESS);
  119.     }
  120.   /* Go read next character. */
  121.   }
  122. fclose(log_file_to_check);
  123. return(ERRORLEVEL_SCAN_FAILED);
  124. }
  125.  
  126. int print_new_part_of_file(char wait_argument[])
  127. {
  128. char the_char;
  129. int wait_time;
  130. mark_holding_file = fopen(mark_holding_filespec,"rt");
  131. if (mark_holding_file == NULL)
  132.   return(ERRORLEVEL_BAD_PARAMETER);
  133. fgets(log_file_spec,sizeof(log_file_spec),mark_holding_file);
  134. log_file_spec[strlen(log_file_spec)-1] = 0;
  135. fgets(temp_string,sizeof(temp_string),mark_holding_file);
  136. temp_string[strlen(temp_string)-1] = 0;  /* Cut off LF. */
  137. file_size_marked = atol(temp_string);
  138. fclose(mark_holding_file);
  139. log_file_to_check = fopen(log_file_spec,"rb");
  140. if (log_file_to_check == NULL)
  141.   {
  142.   printf("?Log file %s not found.\n");
  143.   return(ERRORLEVEL_BAD_PARAMETER);
  144.   }
  145. fseek(log_file_to_check,0,SEEK_END);
  146. file_size_current = ftell(log_file_to_check);
  147. if (file_size_current <= file_size_marked)
  148.   return(ERRORLEVEL_SCAN_FAILED);
  149. fseek(log_file_to_check,file_size_marked,SEEK_SET);
  150. while (ftell(log_file_to_check) < file_size_current)
  151.   {
  152.   fread(&the_char,1,1,log_file_to_check);  /* Read 1 character. */
  153.   printf("%c",the_char);
  154.   }
  155. fclose(log_file_to_check);
  156. wait_time = atoi(wait_argument);  /* How long to wait after showing log. */
  157. if (wait_time)
  158.   wait(wait_time);
  159. return(ERRORLEVEL_SUCCESS);
  160. }
  161.  
  162.  
  163. /*--------------------------- The Code -------------------------------------*/
  164. int main(int argc, char *argv[])
  165. {
  166.  
  167. if (argc != 3)
  168.   {
  169.   print_help_message();
  170.   return(ERRORLEVEL_BAD_PARAMETER);
  171.   }
  172. if (!strcmp(argv[1],"-m"))
  173.   {
  174.   print_header();
  175.   return(mark_file_size(argv[2]));
  176.   }
  177. if (!strcmp(argv[1],"-s"))
  178.   {
  179.   print_header();
  180.   return(scan_file_for_string(argv[2]));
  181.   }
  182. if (!strcmp(argv[1],"-p"))
  183.   return(print_new_part_of_file(argv[2]));
  184. return(ERRORLEVEL_BAD_PARAMETER);
  185. }
  186.  
  187.